<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script src="../js/vue.js"></script>
    <div id="root">
        <h2>人员列表</h2>
        <input type="text" v-model="keyWord" placeholder="请输入名字">
        <ul>
            <!-- v-for遍历数组 -->
            <li v-for="(item,index) of filpersons" :key="item.id">
                {{item.name}}-{{item.age}}
            </li>
        </ul>
    </div>
    <script>
        new Vue({
            el:'#root',
            data(){
                return{
                    num:1,
                    keyWord:'',
                    persons:[
                        {id:'001',name:'马冬梅',age:19,sex:'女'},
                        {id:'002',name:'周冬雨',age:20,sex:'女'},
                        {id:'003',name:'周杰伦',age:21,sex:'男'},
                        {id:'004',name:'温兆伦',age:22,sex:'男'}
                    ],
                    filpersons:[

                    ]
                }
            },
            methods: {
            },
            watch:{
                keyWord:{
                    immediate:true,
                    // 因为字符串默认包含空格
                    handler(newValue){
                        // 过滤
                                            // 获取person的对象
                        this.filpersons =  this.persons.filter((p) => {
                            // 过滤出匹配的条件
                            // return p.name包含newValue
                                        // indexOf方法:这个是判断字符出现的的索引位置，没有就返回-1
                                        // include可以直接反馈 true
                            return p.name.indexOf(newValue) != -1
                            // 使用filters不会对原数组进行更改,会返回一个新的符合规则的数组
                        })
                    }

                },
            }
        })
    </script>
</body>
</html>